// pages/stat-details/stat-details.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    params:{},
    currentDate: new Date().getTime(),
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    const { foodId, unitPrice, marketmxId} = JSON.parse(options.data)
    const [bgeinDate, endDate] = this.getDate()
    this.setData({ 'params.foodId': foodId,
      'params.foodId': foodId,
      'params.unitPrice': unitPrice,
      'params.bgeinDate': bgeinDate,
      'params.endDate': endDate,
    },()=>{
      this.getDetails()
    })
    app.reqFetch.selectDetails({
      marketmxId
    }, (res) => {
      console.log(res[0])
      this.setData({
        food: res[0],
      })
      if (res[0].photo) {
        this.setData({
          imgList: res[0].photo.split(','),
        })

      }
    })
  },
  showStart() {
    this.setData({
      startShow: true
    })
  },
  showEnd() {
    this.setData({
      endShow: true
    })
  },
  onClose() {
    this.setData({
      startShow: false
    })
    this.setData({
      endShow: false
    })
  },
  startConfirm(e) {
    this.setData({
      'params.bgeinDate': this.formatDate(e.detail),
      startShow: false
    }, () => {
    
    })
  },
  endConfirm(e) {
    this.setData({
      'params.endDate': this.formatDate(e.detail),
      endShow: false
    }, () => {
   
    })
  },
  formatDate(date) {
    date = new Date(date)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return year + '-' + month + '-' + day
  },
  getDetails(){
    app.reqFetch.slectCompare({
      ...this.data.params
    }, (res) => {
      console.log(res)
      const { list, totalNum}=res
      this.setData({ list, totalNum})
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getDate() {
    var nowDate = new Date();
    var cloneNowDate = new Date();
    var fullYear = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1; //
    var endOfMonth = new Date(fullYear, month, 0).getDate(); // 获取本月最后一天
    function getFullDate(targetDate) {
      var D, y, m, d;
      if (targetDate) {
        D = new Date(targetDate);
        y = D.getFullYear();
        m = D.getMonth() + 1;
        d = D.getDate();
      } else {
        y = fullYear;
        m = month;
        d = date;
      }
      m = m > 9 ? m : '0' + m;
      d = d > 9 ? d : '0' + d;
      return y + '-' + m + '-' + d;
    };
    var endDate = getFullDate(cloneNowDate.setDate(endOfMonth)); //当月最后一天
    var starDate = getFullDate(cloneNowDate.setDate(1)); //当月第一天
    return [starDate, endDate]
  },
  clickImg: function (e) {

    const entityPhoto = e.currentTarget.dataset.record
    const { imgList } = this.data
    wx.previewImage({
      urls: imgList, //需要预览的图片http链接列表，注意是数组
      current: entityPhoto, // 当前显示图片的http链接，默认是第一个
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
})